#include <iostream>
#include <vcruntime.h>

template <typename T> class Vector {
public:
  Vector(size_t size = 10) {
    _first = new T[size];
    _last = _first;
    _end = _first + size;
  }

  ~Vector() {
    delete[] _first;
    _first = _last = _end = nullptr;
  }

  Vector(const Vector<T> &other) {
    int cap = other._end - other._first;
    int len = other._last - other._first;
    _first = new T[cap];
    for (int i = 0; i < len; i++) {
      _first[i] = other._first[i];
    }
    _last = _first + len;
    _end = _first + cap;
  }

  Vector(Vector<T> &&other) {
    _first = other._first;
    _last = other._last;
    _end = other._end;
    other._first = other._last = other._end = nullptr;
  }

  Vector<T> &operator=(const Vector<T> &other) {
    if (this == &other) {
      return *this;
    }
    delete[] _first;
    int cap = other._end - other._first;
    int len = other._last - other._first;
    _first = new T[cap];
    for (int i = 0; i < len; i++) {
      _first[i] = other._first[i];
    }
    _last = _first + len;
    _end = _first + cap;
    return *this;
  }

  Vector<T> &operator=(Vector<T> &&other) {
    if (this == &other) {
      return *this;
    }
    delete[] _first;
    _first = other._first;
    _last = other._last;
    _end = other._end;
    other._first = other._last = other._end = nullptr;
    return *this;
  }

  T &operator[](size_t idx) { return _first[idx]; }

  void push_back(const T &val) {
    if (_last == _end)
      resize();
    *_last = val;
    _last++;
  }

  void pop_back() {
    if (_last == _first)
      return;
    --_last;
  }

  T back() const { return *(_last - 1); }
  size_t size() const { return _last - _first; }
  bool empty() const { return _last == _first; }

private:
  void resize() {
    int cap = _end - _first;
    T *ptmp = new T[cap * 2];
    for (int i = 0; i < cap; i++) {
      ptmp[i] = _first[i];
    }
    delete[] _first;
    _first = ptmp;
    _last = _first + cap;
    _end = _first + 2 * cap;
  }

private:
  T *_first;
  T *_last;
  T *_end;
};

int main() {
  Vector<int> vec;
  for (int i = 0; i < 11; i++) {
    vec.push_back(i);
  }

  for (int i = 0; i < vec.size(); i++) {
    std::cout << vec[i] << " ";
  }
  std::cout << std::endl;


  Vector<int> vec2{vec};
  while (!vec2.empty()) {
    std::cout << vec2.back() << " ";
    vec2.pop_back();
  }
  std::cout << std::endl;
}